/**
 * FileName: CryptoTool.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-3 上午11:29:32
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-3 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.utils;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * @ClassName: CryptoTool
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-12-3 上午11:29:32
 * 
 */
public class CryptoTool {

	private static final String HEX = "0123456789ABCDEF";

	public static String encrypt(String pKey, String pSimpleText) throws Exception {
		byte[] rawKey = getRawKey(pKey.getBytes());
		byte[] result = encrypt(rawKey, pSimpleText.getBytes());
		return toHex(result);
	}

	public static String decrypt(String pKey, String pEncrypted) throws Exception {
		byte[] rawKey = getRawKey(pKey.getBytes());
		byte[] enc = toByte(pEncrypted);
		byte[] result = decrypt(rawKey, enc);
		return new String(result);
	}

	private static byte[] getRawKey(byte[] pSeed) throws Exception {
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
		sr.setSeed(pSeed);
		keyGen.init(128, sr);
		SecretKey skey = keyGen.generateKey();
		byte[] raw = skey.getEncoded();
		return raw;
	}

	private static byte[] encrypt(byte[] pRaw, byte[] pClear) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(pRaw, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(1, skeySpec);
		return cipher.doFinal(pClear);
	}

	private static byte[] decrypt(byte[] pRaw, byte[] pEncrypted) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(pRaw, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(2, skeySpec);
		byte[] decrypted = cipher.doFinal(pEncrypted);
		return decrypted;
	}

	public static String toHex(String pTxt) {
		return toHex(pTxt.getBytes());
	}

	public static String fromHex(String pHex) {
		return new String(toByte(pHex));
	}

	public static byte[] toByte(String pHexString) {
		int len = pHexString.length() / 2;
		byte[] result = new byte[len];
		for (int i = 0; i < len; i++) {
			result[i] = Integer.valueOf(pHexString.substring(2 * i, 2 * i + 2), 16).byteValue();
		}
		return result;
	}

	public static String toHex(byte[] pBuf) {
		if (pBuf == null) {
			return "";
		}
		StringBuffer result = new StringBuffer(2 * pBuf.length);
		for (int i = 0; i < pBuf.length; i++) {
			appendHex(result, pBuf[i]);
		}
		return result.toString();
	}

	private static void appendHex(StringBuffer pSb, byte pB) {
		pSb.append("0123456789ABCDEF".charAt(pB >> 4 & 0xF)).append("0123456789ABCDEF".charAt(pB & 0xF));
	}

}
